package com.common.utils.classload;

import cn.hutool.core.util.ClassLoaderUtil;
import org.junit.Test;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author xin.ding
 * @date 2021/5/20 14:45
 */
public class Hello {

    public Hello() {
        System.out.println("hello!!!!!!!!!");
    }


    @Test
    public void urlClassLoaderTest() throws Exception {
        File file = new File("d:/");
        URL url = file.toURI().toURL();
        ClassLoader loader = new URLClassLoader(new URL[]{url});
        Class<?> clazz = loader.loadClass("com.common.utils.classload.Hello");
        System.out.println("当前类加载器" + clazz.getClassLoader());
        System.out.println("父类加载器" + clazz.getClassLoader().getParent());
        clazz.newInstance();

    }


    @Test
    public void urlClassLoaderTest1() throws Exception {
        File file = new File("d:/");
        URL url = file.toURI().toURL();
        //设为null表示由根加载器加载
        ClassLoader loader = new URLClassLoader(new URL[]{url});
        Class<?> clazz = loader.loadClass("com.common.utils.classload.Hello");
        System.out.println("当前类加载器" + clazz.getClassLoader());
        System.out.println("父类加载器" + clazz.getClassLoader().getParent());
        clazz.newInstance();

    }

}
